'use strict';

const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;

const db_name_like = 'star-community-like';
const db_like = db.collection(db_name_like);

exports.main = async (event, context) => {
	const flag = event.flag;

	if (flag === 0) { // id查询点赞
		const {
			id,
		} = event.data;
		return await db_like.aggregate()
			.match({
				id,
			})
			.end();
	} else if (flag === 1) { // 添加点赞
		const {
			id,
			user_id,
			ta_id,
			type, // 点赞类型：0帖子，1评论
			enmu_id, // 点赞类型对应的id
		} = event.data;
		return await db_like.add({
			id,
			user_id,
			ta_id,
			type,
			enmu_id,
		});
	} else if (flag === 2) { // 删除点赞
		const {
			user_id,
			enmu_id,
		} = event.data;
		return await db_like.where({
			user_id: user_id,
			enmu_id: enmu_id,
		}).remove();
	}
};